package com.example.hotelsystem.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.hotelsystem.entity.*;
import com.example.hotelsystem.result.Result;
import com.example.hotelsystem.service.*;
import com.example.hotelsystem.vo.floor.FloorCardVO;
import com.example.hotelsystem.vo.room.RoomCardVO;
import com.example.hotelsystem.vo.roomtype.RoomTypeCardVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;


import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Lee
 * @since 2021-03-15
 */
@RestController
@Api(tags = "首页轮播图控制器")
@RequestMapping("/hotelsystem/tHomePicture")
public class THomePictureController {

    @Autowired
    private THomePictureService tHomePictureService;

    @Autowired
    private FloorService floorService;

    @Autowired
    private FloorDetailsService floorDetailsService;

    @Autowired
    private RoomService roomService;

    @Autowired
    private HotelNameService hotelNameService;

    @Autowired
    private RoomTypeService roomTypeService;

    @Autowired
    private CityService cityService;

    @Autowired
    private RoomTypeImageService imageService;


    @PostMapping("/homeView")
    @ApiOperation("轮播图详情")
    public Result HomePicView(Long id){
        try {
            THomePicture tHomePicture = tHomePictureService.getById(id);
            return Result.success(tHomePicture);
        }catch (Exception e){
            return Result.error(e.getMessage());
        }
    }


    @PostMapping("/roomTypeCard")
    @ApiOperation("房间类型卡片")
    public Result roomTypeCard(Long cityId){
        try {
            QueryWrapper<RoomType> checkType = new QueryWrapper<>();

            if(cityId!=null){
                QueryWrapper<Room> cityQuery = new QueryWrapper<>();
                cityQuery.eq("city_id",cityId);

                List<Room> rooms = roomService.list(cityQuery);
                if(rooms==null||rooms.size()==0){
                    return Result.success("该城市还未开放房间");
                }
                List<Long> rtList = new ArrayList<>();
                for(Room item : rooms){
                    rtList.add(item.getRoomtypeId());
                }
                checkType.in("id",rtList);
            }

            checkType.eq("is_delete",0);
            List<RoomTypeCardVO> voList = new ArrayList<>();
            List<RoomType> roomTypes = roomTypeService.list(checkType);
            if(roomTypes==null){
                return Result.error("没有房间类型");
            }
            for(RoomType item : roomTypes){
                RoomTypeCardVO vo = new RoomTypeCardVO();
                BeanUtils.copyProperties(item,vo);
                vo.setRoomTypeId(item.getId());
                QueryWrapper<RoomTypeImage> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("room_type_id",item.getId());
                List<RoomTypeImage> imageList = imageService.list(queryWrapper);
                List<String> url = new ArrayList<>();
                for(RoomTypeImage imageItem : imageList){
                    url.add(imageItem.getImage());
                }
                vo.setRoomTypeName(item.getType());
                vo.setUrl(url);

                QueryWrapper<Room> roomQuery = new QueryWrapper<>();
                roomQuery.eq("roomtype_id",item.getId());
                List<Room> rooms = roomService.list(roomQuery);
                List<Long> cityIdList = new ArrayList<>();
                for(Room r : rooms){
                    cityIdList.add(r.getCityId());
                }

                if(cityIdList!=null&&cityIdList.size()>0){
                    //去除cityList中重复的数据
                    Set set = new  HashSet();
                    List<Long> newList = new  ArrayList();
                    for (Long cd:cityIdList) {
                        if(set.add(cd)){
                            newList.add(cd);
                        }
                    }
                    String name = "";
                    for(Long id :newList){
                        City c = cityService.getById(id);
                        name+=c.getCityName()+",";
                    }
                    name = name.substring(0,name.length()-1);
                    vo.setCityName(name);
                }


                if(cityId!=null){
                    City c =cityService.getById(cityId);
                    vo.setCityName(c.getCityName());
                }




                voList.add(vo);
            }
            return Result.success(voList);
        }catch (Exception e){
            return Result.error(e.getMessage());
        }
    }

    @PostMapping("/roomCard")
    @ApiOperation("房间卡片")
    public Result roomCard(Long cityId){
        try{
            QueryWrapper<Room> cityQuery = new QueryWrapper<>();

            if(cityId!=null){
                cityQuery.eq("city_id",cityId);
            }

            List<RoomCardVO> voList = new ArrayList<>();
            List<Room> rooms = roomService.list(cityQuery);

            if(rooms==null){
                return Result.success("暂无房间");
            }

            for(Room item : rooms){
                RoomCardVO vo = new RoomCardVO();
                vo.setRoomId(item.getId());
                vo.setRoomNum(item.getNumber());

                HotelName ht = hotelNameService.getById(item.getHotelId());
                vo.setHotelName(ht.getHotelName());

                City ct = cityService.getById(item.getCityId());
                vo.setCityName(ct.getCityName());

                RoomType rt = roomTypeService.getById(item.getRoomtypeId());

                QueryWrapper<RoomTypeImage> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("room_type_id",item.getRoomtypeId());
                List<RoomTypeImage> imageList = imageService.list(queryWrapper);
                List<String> url = new ArrayList<>();
                for(RoomTypeImage imageItem : imageList){
                    url.add(imageItem.getImage());
                }
                vo.setPrice(rt.getPrice());
                vo.setUrl(url);
                vo.setRoomTypeName(rt.getType());
                voList.add(vo);
            }
            return Result.success(voList);
        }catch (Exception e){
            return Result.error(e.getMessage());
        }
    }


    @PostMapping("/floorCard")
    @ApiOperation("楼层卡片")
    public Result floorCard(){
        try {
            List<FloorCardVO> voList = new ArrayList<>();
            int count = 0 ;

            QueryWrapper<Floor> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("status",1);
            List<Floor> floors = floorService.list(queryWrapper);
            for(Floor item : floors){
                if(count==4){
                    break;
                }
                FloorCardVO vo = new FloorCardVO();
                vo.setFloorId(item.getId());
                vo.setFloorName(item.getName());

                QueryWrapper<FloorDetails> q1 = new QueryWrapper<>();
                q1.eq("floor_id",item.getId());
                FloorDetails floorDetails = floorDetailsService.getOne(q1);
                if(floorDetails.getImage()!=null){
                    vo.setUrl(floorDetails.getImage());
                }
                vo.setContent(floorDetails.getContent());
                voList.add(vo);
                count++;
            }
            return Result.success(voList);
        }catch (Exception e){
            return Result.error(e.getMessage());
        }
    }

    @PostMapping("/list")
    @ApiOperation("顶部轮播图")
    public Result list(){
        try {
            List<THomePicture> list = tHomePictureService.list();
            for(THomePicture item : list){
                item.setIntroduce(null);
            }
            return Result.success(list);
        }catch (Exception e){
            return Result.error(e.getMessage());
        }
    }

    @PostMapping("/add")
    @ApiOperation("添加首页轮播图")
    public Result add(THomePicture tHomePicture){
        try {
            Date date = new Date();
            tHomePicture.setCreateTime(date);
            boolean isAdd = tHomePictureService.save(tHomePicture);
            if(isAdd){
                return Result.success("操作成功");
            }
            return Result.error("操作失败");
        }catch (Exception e){
            return Result.error(e.getMessage());
        }
    }
}

